package kr.pe.johnkim.carrotpoint.mobile.controller;

import kr.pe.johnkim.carrotpoint.mobile.domain.CarrotPointMerchantPayment;
import kr.pe.johnkim.carrotpoint.mobile.service.CarrotPointMerchantPaymentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Carrot Point 가맹점 결제 관련 controller 클래스 이다.
 *
 * @author : John_Kim <cale9797@gmail.com>
 * @date : 13. 2. 6.
 */
@Controller
@RequestMapping(value = "/carrotPointMerchantPaymentController")
public class CarrotPointMerchantPaymentController {

    // SLF4J Logging
    private Logger logger = LoggerFactory.getLogger(CarrotPointMerchantPaymentController.class);

    @Autowired
    private CarrotPointMerchantPaymentService carrotPointMerchantPaymentService;

    /**
     * 가맹점 결제를 등록 한다.
     *
     * @param carrotPointMerchantPayment
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/registerMerchantPayment", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public CarrotPointMerchantPayment registerMerchantPayment
            (@RequestBody CarrotPointMerchantPayment carrotPointMerchantPayment) throws Exception {
        logger.info("registerMerchantPayment");

        CarrotPointMerchantPayment result = new CarrotPointMerchantPayment();

        try {
            carrotPointMerchantPaymentService.registerMerchantPayment(carrotPointMerchantPayment);
            result.setResult("SUCCESS");
            logger.info("success to register merchant payment.");
        } catch (Exception e) {
            result.setResult("FAIL");
            logger.error("fail to register merchant payment.");
        }

        return result;
    }

    /**
     * 가맹점 결제 목록을 가져 온다.
     *
     * @param carrotPointMerchantPayment
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getMerchantPaymentList", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public List<CarrotPointMerchantPayment> getMerchantPaymentList
            (@RequestBody CarrotPointMerchantPayment carrotPointMerchantPayment) throws Exception {
        logger.error("getMerchantPaymentList");

        logger.info("carrotPointMerchantMerchantId : {}", carrotPointMerchantPayment.getCarrotPointMerchantMerchantId());
        logger.info("activateYn                    : {}", carrotPointMerchantPayment.getActivateYn());

        return carrotPointMerchantPaymentService.getMerchantPaymentList(carrotPointMerchantPayment);
    }

    /**
     * 가맹점 결제 내용을 가져 온다.
     *
     * @param carrotPointMerchantPayment
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getMerchantPaymentInformation", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public CarrotPointMerchantPayment getMerchantPaymentInformation
            (@RequestBody CarrotPointMerchantPayment carrotPointMerchantPayment) throws Exception {
        logger.info("getMerchantPaymentInformation");

        logger.info("idCarrotPointMerchantPayment : {}", carrotPointMerchantPayment.getIdCarrotPointMerchantPayment());
        logger.info("activateYn                   : {}", carrotPointMerchantPayment.getActivateYn());

        return carrotPointMerchantPaymentService.getMerchantPaymentInformation(carrotPointMerchantPayment);
    }

    /**
     * 가맹점 결제 내용을 업데이트 한다.
     *
     * @param carrotPointMerchantPayment
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateMerchantPaymentInformation", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public CarrotPointMerchantPayment updateMerchantPaymentInformation
            (@RequestBody CarrotPointMerchantPayment carrotPointMerchantPayment) throws Exception {
        logger.info("updateMerchantPaymentInformation");

        CarrotPointMerchantPayment result = new CarrotPointMerchantPayment();

        try {
            carrotPointMerchantPaymentService.updateMerchantPaymentInformation(carrotPointMerchantPayment);
            result.setResult("SUCCESS");
            logger.info("success to update merchant payment information.");
        } catch (Exception e) {
            result.setResult("FAIL");
            logger.error("fail to update merchant payment information.");
        }

        return result;
    }

    /**
     * 가맹점 결제를 비활성화 한다.
     *
     * @param carrotPointMerchantPayment
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateDeactivateMerchantPayment", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public CarrotPointMerchantPayment updateDeactivateMerchantPayment
            (@RequestBody CarrotPointMerchantPayment carrotPointMerchantPayment) throws Exception {
        logger.info("updateDeactivateMerchantPayment");

        CarrotPointMerchantPayment result = new CarrotPointMerchantPayment();

        try {
            carrotPointMerchantPaymentService.updateDeactivateMerchantPayment(carrotPointMerchantPayment);
            result.setResult("SUCCESS");
            logger.info("success to update deactivate merchant payment.");
        } catch (Exception e) {
            result.setResult("FAIL");
            logger.error("fail to update deactivate merchant payment.");
        }

        return result;
    }
}
